﻿package cn.smbms.dao.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.mail.Flags.Flag;
import javax.mail.internet.NewsAddress;

import cn.smbms.dao.BaseDao;
import cn.smbms.pojo.Page;
import cn.smbms.pojo.PaginationSupport;
import cn.smbms.pojo.User;
import cn.smbms.test.userServiceTest;

public class UserDaoImpl implements UserDao {

	@Override
	public int add(Connection connection, User user) throws Exception {
		PreparedStatement pstm = null;
		int updateRows = 0;
		if (null != connection) {
			String sql = "insert into smbms_user (userCode,userName,userPassword,"
					+ "userType,gender,birthday,phone,address,creationDate,createdBy) "
					+ "values(?,?,?,?,?,?,?,?,?,?)";
			Object[] params = { user.getUserCode(), user.getUserName(),
					user.getUserPassword(), user.getUserType(),
					user.getGender(), user.getBirthday(), user.getPhone(),
					user.getAddress(), user.getCreationDate(),
					user.getCreatedBy() };
			updateRows = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return updateRows;
	}

	@Override
	public User getLoginUser(Connection connection, String userCode)
			throws Exception {
		PreparedStatement pstm = null;
		ResultSet rs = null;
		User user = null;
		if (null != connection) {
			String sql = "select * from smbms_user where userCode=?";
			Object[] params = { userCode };
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserCode(rs.getString("userCode"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setGender(rs.getInt("gender"));
				user.setBirthday(rs.getDate("birthday"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setUserType(rs.getInt("userType"));
				user.setCreatedBy(rs.getInt("createdBy"));
				user.setCreationDate(rs.getTimestamp("creationDate"));
				user.setModifyBy(rs.getInt("modifyBy"));
				user.setModifyDate(rs.getTimestamp("modifyDate"));
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return user;
	}

	@Override
	// 查询
	public List<User> getUserList(Connection connection, String userName,int pageIndex,int pageSize)
			throws Exception {
		PreparedStatement pstm = null;
		ResultSet rs = null;
		List<User> userList = new ArrayList<User>();
		if (connection != null) {
			String sql = "select * from smbms_user where userName like ? limit ?,?";
			Page page=new Page();
			page.setCounts(getCounts(userName, connection));
			page.setPageIndex(pageIndex);
			page.setPageSize(pageSize);
			int start =page.getStart();
			int end=page.getEnd();
			Object[] params = { "%" + userName + "%",start,end };
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			while (rs.next()) {
				User _user = new User();
				_user.setId(rs.getInt("id"));
				_user.setUserCode(rs.getString("userCode"));
				_user.setUserName(rs.getString("userName"));
				_user.setGender(rs.getInt("gender"));
				_user.setBirthday(rs.getDate("birthday"));
				_user.setPhone(rs.getString("phone"));
				_user.setUserType(rs.getInt("userType"));

				userList.add(_user);
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return userList;
	}

	@Override
	// 根据id删除
	public boolean deleteUserById(Connection connection, Integer delId)
			throws Exception {
		PreparedStatement pstm = null;
		boolean flag = false;
		if (null != connection) {
			String sql = "delete from smbms_user where id=?";
			Object[] params = { delId };
			if (BaseDao.execute(connection, pstm, sql, params) > 0)
				flag = true;
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	// 根据id查找用户
	public User getUserById(Connection connection, String id) throws Exception {
		User user = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		if (null != connection) {
			String sql = "select * from smbms_user where id=?";
			Object[] params = { id };
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserCode(rs.getString("userCode"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setGender(rs.getInt("gender"));
				user.setBirthday(rs.getDate("birthday"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setUserType(rs.getInt("userType"));
				user.setCreatedBy(rs.getInt("createdBy"));
				user.setCreationDate(rs.getTimestamp("creationDate"));
				user.setModifyBy(rs.getInt("modifyBy"));
				user.setModifyDate(rs.getTimestamp("modifyDate"));
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return user;
	}

	@Override
	// 修改
	public boolean modify(Connection connection, User user) throws Exception {
		boolean flag = false;
		PreparedStatement pstm = null;
		if (null != connection) {
			String sql = "update smbms_user set userName=?,"
					+ "gender=?,birthday=?,phone=?,address=?,userType=?,modifyBy=?,modifyDate=? where id = ? ";
			Object[] params = { user.getUserName(), user.getGender(),
					user.getBirthday(), user.getPhone(), user.getAddress(),
					user.getUserType(), user.getModifyBy(),
					user.getModifyDate(), user.getId() };
			if (BaseDao.execute(connection, pstm, sql, params) > 0) {
				flag = true;
			}
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	// 修改密码
	public boolean updatePwd(Connection connection, int id, String pwd)
			throws Exception {
		boolean flag = false;
		PreparedStatement pstm = null;
		if (connection != null) {
			String sql = "update smbms_user set userPassword= ? where id = ?";
			Object[] params = { pwd, id };
			if (BaseDao.execute(connection, pstm, sql, params) > 0) {
				flag = true;
			}
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	public int getCounts(String userName,Connection connection) {
		int counts = 0;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		if (connection != null) {
			String sql = "SELECT COUNT(*) FROM smbms_user where userName like ?";
			Object[] params = {"%" + userName + "%"};
			try {
				rs = BaseDao.execute(connection, pstm, rs, sql, params);
				while (rs.next()) {
					counts = rs.getInt(1);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return counts;
	}

	

	

}
